package com.sx.shop.controller;

import com.sx.shop.entity.Product;
import com.sx.shop.entity.Store;
import com.sx.shop.entity.StoreProduct;
import com.sx.shop.entity.User;
import com.sx.shop.service.StoreProductService;
import com.sx.shop.service.UserService;
import com.sx.shop.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping(value = "/storeProduct")
public class StoreProductController {
    // DI 依赖注入
    @Autowired
    private StoreProductService storeProductService;

    //@RequestMapping(value = "/index" method="get")
    //指定请求方式
    @GetMapping("/index")
    public String openPage() {
        //这是一个html页面
        return "storeProduct";
    }

    /* 获取某个仓库的商品列表 */
    @ResponseBody//只返回数据不找页面
    @RequestMapping(value = "/list")
    public ResultVo selectStoreProductByStoreId(Integer storeId,String keyword,Integer page , Integer limit){
        //Ioc 控制反转
        //UserService service=new UserService();
        return storeProductService.selectStoreProduct(storeId,keyword,page, limit);
    }

    /* 库存修改记录列表 */
    @ResponseBody//只返回数据不找页面
    @RequestMapping(value = "/changeList")
    public ResultVo selectStoreChangeByStoreId(Integer storeId,String keyword ,Integer page , Integer limit){
        //Ioc 控制反转
        //UserService service=new UserService();
        return storeProductService.selectStoreChangeByStoreId(storeId,keyword,page,limit);
    }

//  仓库内添加新的商品
    @ResponseBody
    @RequestMapping(value = "/add")
    public void addStoreProduct(Integer storeId,Integer productId,Integer num) {
        Store store=new Store();
        store.setId(storeId);
        Product product=new Product();
        product.setId(productId);
        StoreProduct storeProduct=new StoreProduct(store,product,num);
        storeProductService.doAdd(storeProduct);
    }

//  仓库删除商品
    @ResponseBody
    @RequestMapping(value="/del")
    public void delStoreProduct(Integer storeProductId,Integer storeId,Integer productId,Integer num) {
        Store store=new Store();
        store.setId(storeId);
        Product product=new Product();
        product.setId(productId);
        StoreProduct storeProduct=new StoreProduct(storeProductId,store,product,num);
        storeProductService.doDel(storeProduct);
    }

    /*修改某个仓库里某个商品的数量*/
    @ResponseBody
    @RequestMapping(value="/save")
    /*
    * num:最终改成了多少
    * changeNum:改了多少 >0 购入    <0 销售出*/
    public void saveStoreProduct(Integer storeProductId,Integer storeId,Integer productId,Integer num,Integer changeNum) {
        Store store=new Store();
        store.setId(storeId);
        Product product=new Product();
        product.setId(productId);
        StoreProduct storeProduct=new StoreProduct(storeProductId,store,product,num);
        storeProductService.doSave(storeProduct,changeNum);
    }
}
